#include "StdAfx.h"
#include "list.h"
/************************************************************************************************/
#define newentry(en,da,ne)\
	en = new entry_t;\
	en->data = da;\
	en->next = ne;
#define delentry(en)\
	{\
	entry_t *tmp = en->next;\
	delete en->data;\
	delete en;\
	en = tmp;\
	}
#define delentry_notdata(en)\
	{\
	entry_t *tmp = en->next;\
	delete en;\
	en = tmp;\
	}
/************************************************************************************************/
list_c::list_c()
{
firstentry = 0;
lastentry = 0;
count = 0;
}
/************************************************************************************************/
list_c::~list_c()
{
clear();
}
/************************************************************************************************/
void list_c::add(void *data)
{
if(firstentry == 0) //no entries yet
	{
	newentry(firstentry,data,0);
	lastentry = firstentry;
	}
else
	{
	entry_t *e;

	newentry(e,data,0);
	lastentry->next = e; //make the last entry's next point to the new one
	lastentry = e; //its the last entry now
	}
count++;
}
/************************************************************************************************/
void list_c::ins(void *data,int n)
{
int i;

if(n >= num()) //position greater than entries
	add(data);
else if(firstentry == 0) //no entries yet
	add(data);
else if(n == 0) //insert as first entry
	{
	entry_t *tmp = firstentry; //now its secondentry :P

	newentry(firstentry,data,tmp);
	}
else
	{
	entry_t *eseek = firstentry,*e;

	for(i=0;i<(n-1);i++) //skip to position right before where it goes
		eseek = eseek->next;
	newentry(e,data,eseek->next); //set the new entry's next to entry n
	eseek->next = e; //set entry n to the new entry
	if(e->next == 0) //we just added a last entry, fix lastentry
		lastentry = e;
	}
count++;
}
/************************************************************************************************/
void list_c::del(int n) //zero based index
{
int i;
entry_t *e = firstentry;//,*tmp;

if(n >= num()) //non existant item
	return;
else if(n == 0) //delete entry 0
	{
	delentry(firstentry);
	if(firstentry == 0) //no more entries
		lastentry = 0;
	else if(firstentry->next == 0) //we just deleted last entry, fix lastentry
		lastentry = firstentry;
	}
else
	{
	for(i=0;i<(n-1);i++) //skip to entry before the one to be deleted (delete e->next after loop)
		e = e->next;
	delentry(e->next);
	if(e->next == 0) //we just deleted last entry, fix lastentry
		lastentry = e;
	}
count--;
}
/************************************************************************************************/
void list_c::del_savedata(int n) //zero based index
{
int i;
entry_t *e = firstentry;//,*tmp;

if(n >= num()) //non existant item
	return;
else if(n == 0) //delete entry 0
	{
	delentry_notdata(firstentry);
	if(firstentry == 0) //no more entries
		lastentry = 0;
	else if(firstentry->next == 0) //we just deleted last entry, fix lastentry
		lastentry = firstentry;
	}
else
	{
	for(i=0;i<(n-1);i++) //skip to entry before the one to be deleted (delete e->next after loop)
		e = e->next;
	delentry_notdata(e->next);
	if(e->next == 0) //we just deleted last entry, fix lastentry
		lastentry = e;
	}
count--;
}
/************************************************************************************************/
void list_c::clear()
{
entry_t *e = firstentry;

while(e) //while there is still an entry
	{
	entry_t *tmp = e->next; //make tmp point to next entry

	delete e->data; //delete data
	delete e; //delete entry
	e = tmp; //now make e equal tmp (the next entry)
	}
firstentry = 0;
lastentry = 0;
count = 0;
}
/************************************************************************************************/
void *list_c::get(int n) //get data for entry n (0 = first, -1 = last)
{
int i;
entry_t *e = firstentry;

if(n == -1)
	return(lastentry->data);
if(n >= num())
	return(0);
for(i=0;i<n;i++)
	e = e->next;
return(e->data);
}
/************************************************************************************************/
int list_c::num()
{
return(count);
}
/************************************************************************************************/
